from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import declarative_base
from common.config import config

# 数据库连接 'mysql+pymysql://用户名:密码@服务器地址:端口号/数据库名'
SQLALCHEMY_DBURI=f'mysql+pymysql://'\
    f'{config.username}'\
    f':{config.password}'\
    f'@{config.server}'\
    f':{config.port}'\
    f'/{config.dbname}'\
    f'?charset=utf8mb4'
# 创建数据库引擎
create_engine = create_engine(SQLALCHEMY_DBURI, pool_pre_ping=True)

# 创建会话工厂，用于后续的数据库操作
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=create_engine)

# 声明式模型基类，用于定义数据库模型
Base = declarative_base()